package com.cqeec.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;

import com.cqeec.bean.UserInfo;

/**
 * 
 * @author HuaL
 * @description 主窗体
 */
public class MainFrame extends JFrame implements ActionListener {
	static UserInfo userInfo;
	public static JDesktopPane deskPane = new JDesktopPane();
	// 菜单
	JMenuBar menuBar = new JMenuBar();
	JMenu system = new JMenu("系统(S)");
	JMenuItem editPwdItem = new JMenuItem("修改密码");
	JMenuItem outItem = new JMenuItem("退出");

	JMenu book = new JMenu("图书管理(B)");
	JMenuItem bookItem = new JMenuItem("图书信息列表");

	JMenu reader = new JMenu("读者管理(R)");
	JMenuItem readerItem = new JMenuItem("读者信息列表");

	JMenu lending = new JMenu("借阅管理(J)");
	JMenuItem lendingItem = new JMenuItem("借阅信息列表");
	JMenu statisticsItem = new JMenu("借阅统计");
	JMenuItem historyItem = new JMenuItem("历史借阅统计");
	JMenuItem ranklistItem = new JMenuItem("图书排行榜统计");
	
	JMenu help = new JMenu("帮助(H)");
	JMenuItem aboutItem = new JMenuItem("关于我们");

	// 工具条
	JToolBar toolBar = new JToolBar();
	JButton editpwdBtn = new JButton();
	JButton booklistBtn = new JButton();
	JButton readerlistBtn = new JButton();
	JButton lendinglistBtn = new JButton();
	JButton aboutBtn = new JButton();
	JButton exitBtn = new JButton();

	public MainFrame(UserInfo userInfo) {
		this.userInfo = userInfo;
		Init();
		setMenuAuthority(userInfo.getUsertype());
	}

	public void Init() {
		setLayout(null);
		setSize(1024, 768);
		setTitle("博研图书馆管理系统");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		InitMenu();
		InitToolBar();
		deskPane.setBackground(Color.gray);
		add(deskPane);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				int i = JOptionPane.showConfirmDialog(null, "是否确认要退出本系统？", "确认",
						JOptionPane.YES_NO_OPTION);
				if (i != JOptionPane.YES_OPTION){
					setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
				}else{
					setDefaultCloseOperation(EXIT_ON_CLOSE);
					super.windowClosing(e);
				}
			}
		});
		setVisible(true);
	}
	
	/**
	 * 根据用户类型设置菜单权限
	 * @param userType
	 */
	public void setMenuAuthority(int userType){
		//1：系统管理员  2：图书管理员  3：借阅管理员
		if (userType == 2){
			reader.setVisible(false);
			lending.setVisible(false);
			readerlistBtn.setVisible(false);
			lendinglistBtn.setVisible(false);
			
		}
		if (userType == 3){
			book.setVisible(false);
			reader.setVisible(false);
			booklistBtn.setVisible(false);
			readerlistBtn.setVisible(false);
		}
	}
	
	public void InitMenu() {
		// 菜单设置
		menuBar.add(system);
		system.setMnemonic(KeyEvent.VK_F);// 添加快捷键
		system.add(editPwdItem);
		editPwdItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
				ActionEvent.CTRL_MASK));// 添加快捷键
		editPwdItem.addActionListener(this);

		system.addSeparator();// 添加分割线

		system.add(outItem);
		outItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
				ActionEvent.CTRL_MASK));
		outItem.addActionListener(this);

		menuBar.add(book);
		book.setMnemonic(KeyEvent.VK_B);
		book.add(bookItem);
		bookItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,
				ActionEvent.CTRL_MASK));
		bookItem.addActionListener(this);

		menuBar.add(reader);
		reader.setMnemonic(KeyEvent.VK_R);

		reader.add(readerItem);
		readerItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
				ActionEvent.CTRL_MASK));// 添加快捷键
		readerItem.addActionListener(this);

		menuBar.add(lending);
		lending.setMnemonic(KeyEvent.VK_L);
		lending.add(lendingItem);
		lendingItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L,
				ActionEvent.CTRL_MASK));
		lending.addActionListener(this);
		lending.add(statisticsItem);
		statisticsItem.add(historyItem);
		statisticsItem.add(ranklistItem);
		
		
		historyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M,
				ActionEvent.CTRL_MASK));
		historyItem.addActionListener(this);
		ranklistItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
				ActionEvent.CTRL_MASK));
		ranklistItem.addActionListener(this);
		menuBar.add(help);
		help.setMnemonic(KeyEvent.VK_H);
		help.add(aboutItem);
		aboutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
				ActionEvent.CTRL_MASK));
		aboutItem.addActionListener(this);

		this.setJMenuBar(menuBar);// 给窗体设置菜单
	}

	public void InitToolBar() {
		// 工具条设置
		editpwdBtn.setToolTipText("修改密码");
		editpwdBtn.setIcon(new ImageIcon("ico\\editpwd.jpg"));
		editpwdBtn.addActionListener(this);
		toolBar.add(editpwdBtn);

		booklistBtn.setToolTipText("图书信息列表");
		booklistBtn.setIcon(new ImageIcon("ico\\book.jpg"));
		booklistBtn.addActionListener(this);
		toolBar.add(booklistBtn);

		readerlistBtn.setToolTipText("读者信息列表");
		readerlistBtn.setIcon(new ImageIcon("ico\\reader.jpg"));
		readerlistBtn.addActionListener(this);
		toolBar.add(readerlistBtn);

		lendinglistBtn.setToolTipText("借阅信息列表");
		lendinglistBtn.setIcon(new ImageIcon("ico\\lending.jpg"));
		lendinglistBtn.addActionListener(this);
		toolBar.add(lendinglistBtn);
		
		aboutBtn.setToolTipText("关于我们");
		aboutBtn.setIcon(new ImageIcon("ico\\about.jpg"));
		aboutBtn.addActionListener(this);
		toolBar.add(aboutBtn);
		
		exitBtn.setToolTipText("退出系统");
		exitBtn.setIcon(new ImageIcon("ico\\exit.jpg"));
		exitBtn.addActionListener(this);
		toolBar.add(exitBtn);

		this.setLayout(new BorderLayout());
		toolBar.setFloatable(false);
		this.add(toolBar, BorderLayout.NORTH);
	}


	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == editPwdItem || e.getSource() == editpwdBtn) {
			EditPasswordFrame.show(deskPane);// 弹出修改密码窗体
		}
		if (e.getSource() == exitBtn || e.getSource() == outItem) {
			int i = JOptionPane.showConfirmDialog(null, "是否确认要退出本系统？", "确认",
					JOptionPane.YES_NO_OPTION);
			if (i == JOptionPane.YES_OPTION) {
				System.exit(0);// 退出系统
			}
		}
		if (e.getSource() == booklistBtn || e.getSource() == bookItem) {
			BookFrame.show(deskPane);// 弹出图书信息窗体
		}
		if (e.getSource() == readerlistBtn || e.getSource() == readerItem) {
			ReaderFrame.show(deskPane);// 弹出读者信息窗体
		}
		if (e.getSource() == lendinglistBtn || e.getSource() == lendingItem) {
			// 弹出借阅信息窗体
			LendingFrame.show(deskPane);
		}
		if (e.getSource() == aboutBtn || e.getSource() == aboutItem) {
			// 弹出关于我们
			AboutFrame about = new AboutFrame(this, "关于我们", true);
			about.show();
		}
		if (e.getSource() == historyItem){
			//历史借阅统计
			HsyLendTJFrame.show(deskPane);
		}
		if (e.getSource() == ranklistItem){
			//图书借阅排行榜统计
			BookRanklListFrame.show(deskPane);
		}
	}
}
